<?php

namespace App\Models\Back;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Intervention\Image\ImageManager;

class __MODEL__ extends Model
{
	protected $table = '__TABLE__';
	/*
	 * 设置
	 * @param   $request    array   表单数据数组
	 * return   boot    数据库更新成功返回true否则返回false
	 */
    public function set($request)
    {
    	if (isset($request['id'])) {
    		$fields = self::findOrFail($request['id']);
	    } else {
		    $fields = $this;
	    }

    	foreach ($request as $key => $value) {
    		$fields->$key = $value;
	    }
	    unset($fields->__token);
	    return $fields->save();
    }

    /*
     * 添加或修改表单验证
     * @param   array   $request    表单数据数组
     * return   mix     验证通过自动返回true，验证失败自动重定向到上一个页面
     */
	public function setValidator($request)
	{

		if (isset($request['id'])) {
			$id = $request['id'];
		} else {
			$id = 0;
		}

		Validator::make($request, [

		])->validate();
	}

	/*
     * 删除表单验证
     * @param   array   $request    表单数据数组
     * return   mix     验证通过自动返回true，验证失败自动重定向到上一个页面
     */
	public function deleteValidator($request)
	{
		Validator::make($request, [
			'selected' => [
				'required',
				'array'
			],
			'selected.*' => [
				'integer'
			]
		])->validate();
	}

	/*
	 * 删除，支持批量删除
	 * @param   array   $idList 要删除的品牌id列表
	 */
	public function delete__MODEL__($idList)
	{
		//删除数据库信息
		return self::destroy($idList);
	}

	/*
	 * 品牌排序问题
	 */
	public function __VIEW__Order($field, $sort)
	{
		$sort = $sort == 'asc' ? $sort : 'desc';
		return self::orderBy($field, $sort)->simplePaginate(config('tool.back.page', 5));
	}
}
